import BaseSprite from "../Base/BaseSprite";

const { ccclass, property } = cc._decorator;

@ccclass
export class CloudAnimation extends BaseSprite {

    @property({ step: 1, tooltip: "起始位置" })
    private pos0: number = 0;

    @property({ step: 1, tooltip: "结束位置" })
    private pos1: number = 0;

    @property({ step: 1, tooltip: "最小速度" })
    private speedMin: number = 0;

    @property({ step: 1, tooltip: "最大速度" })
    private speedMax: number = 0;

    private time: number = 10;
    private scale: number = 1;

    protected onLoad() {
        const cb = () => {
            this.scale = this.countScale();
            this.time = this.countMoveTime();
            cc.tween(this.node).sequence(
                cc.scaleTo(0, this.scale, this.scale),
                cc.moveTo(0, cc.v2(this.pos0, this.node.y)),
                cc.moveTo(this.time, cc.v2(this.pos1, this.node.y)),
                cc.callFunc(() => {
                    cb && cb();
                }),
            ).start();
        }
        cb && cb();
    }

    protected start() { }

    private countScale() {
        return Math.random() * 0.7 + 0.2;
    }

    private countMoveSpeed() {
        const min = this.speedMin;
        const max = this.speedMax;
        const speed = Math.random() * (max - min) + min;
        // console.log("移动速度:", speed);
        return speed;
    }

    private countMoveTime() {
        const start = this.pos0;
        const end = this.pos1;
        const dis = Math.abs(end - start);
        const speed = this.countMoveSpeed();
        // console.log("移动时间:", dis / speed, "距离:", dis, start, end);
        return dis / speed;
    }
}